#include "../includes.h"


#define SYSTEM_ID_X		10
#define SYSTEM_RUN_X    19
#define SYSTEM_ACTIVE_X 36
#define SYSTEM_MODEL_X	8

//#define	HEALTH_SELFTEST_X	18
//#define	HEALTH_POWER_X		31
//#define	HEALTH_EFC_X		42
//#define	HEALTH_RTC_X		53
//#define	HEALTH_ANT_X	64
//#define	HEALTH_FAN_X	75

//#define	HEALTH_SIG_X	16
//#define	HEALTH_POWER_X		27
//#define	HEALTH_EFC_X		38
//#define	HEALTH_RTC_X		49
//#define	HEALTH_ANT_X	60
//#define	HEALTH_FAN_X	71

//#define	HEALTH_DUAL_X	13
//#define	HEALTH_SIG_X	25
//#define	HEALTH_POWER_X		36
//#define	HEALTH_EFC_X		47
//#define	HEALTH_RTC_X		58
//#define	HEALTH_ANT_X	69
//#define	HEALTH_FAN_X	80
#define	HEALTH_DUAL_X	13
#define	HEALTH_SIG_X	24
#define	HEALTH_POWER_X		34
#define	HEALTH_EFC_X		44
#define	HEALTH_RTC_X		54 // ejt, clk
#define	HEALTH_ANT_X	64
#define	HEALTH_FAN_X	74


STATUS_	STATUS;



char STATUS_ASCII[1500];

//kang char SL_MainTitle_P[83+1] 		= "x[1;3H------------------------<<  RCGD STATUS MONITOR  >>------------------------- ";
//kang char SL_MainTitle_S[83+1] 		= "x[1;3H------------------------<<  RCGD STATUS MONITOR  >>------------------------- ";

char SL_MainTitle_P[84+1] 		= "x[1;3H------------------------<<  "Product" STATUS MONITOR  >>------------------------- ";
char SL_MainTitle_S[84+1] 		= "x[1;3H------------------------<<  "Product" STATUS MONITOR  >>------------------------- ";
//char SL_MainTitle_P[87+1] 		= "x[1;3H------------------------<<  "Product" STATUS MONITOR  >>------------------------- ";
//char SL_MainTitle_S[87+1] 		= "x[1;3H------------------------<<  "Product" STATUS MONITOR  >>------------------------- ";

char SL_SystemTitle[69+1] 		= "x[3;3HSYSTEM............................................Running Time ";
//char SL_System[54+1]			= "x[4;3H Module [ ]                                     ";
char SL_System[54+1]			= "x[4;3H                                                ";
char SL_ControlTimeTitle_1[83+1]= "x[7;3HCONTROL_______________________________      TIME____________________________ ";
char SL_ControlTimeTitle_2[83+1]= "x[7;3HCONTROL______________________[invalid]      TIME____________________________ ";
char SL_ControlTimeTitle_3[83+1]= "x[7;3HCONTROL______________________[invalid]      TIME___________________[invalid] ";
char SL_ControlTimeTitle_4[83+1]= "x[7;3HCONTROL_____________________[Holdover]      TIME____________________________ ";
char SL_ControlTimeTitle_5[83+1]= "x[7;3HCONTROL_____________________[Recovery]      TIME____________________________ ";
char SL_ControlTimeTitle_6[83+1]= "x[7;3HCONTROL_____________________[Free-run]      TIME____________________________ ";
char SL_PositionSatTitle[84+1]	= "x[16;3HPOSITION________________________________    SATELLITE_______________________ ";
char SL_PositionSatTitl2[84+1]	= "x[16;3HPOSITION___________________________   SATELLITE_____________________________ ";
char SL_HealthTitle[84+1]		= "x[23;3HHEALTH MONITOR.............................................................. ";
//char SL_Health[83+1]			= "x[24;3H Selftest [    ]  Power [    ]  EFC [    ]  RTC [    ]  Ref [     /       ] ";
//char SL_Health[83+1]			= "x[24;3H Selftest [    ] Power [    ] EFC [    ] RTC [    ] ANT [    ] FAN [    ] ";
//char SL_Health[83+1]			= "x[24;3H    SIG [    ] PWR [    ] EFC [    ] EJT [    ] ANT [    ] FAN [    ] ";
//char SL_Health[85+1]			= "x[24;3HDUAL [    ]  SIG [    ] PWR [    ] EFC [    ] EJT [    ] ANT [    ] FAN [    ]";
char SL_Health[84+1]			= "x[24;3H DUAL[    ]  SIG[    ] PWR[    ] EFC[    ] CLK[    ] ANT[    ] FAN[    ]";

float Init_Temperature;
unsigned char Init_CounterModule_Status;


void STATUS_OUT(int,char);
void FixedStatusScreen(void);
void status_text_out(void);


void Init_STATUS()
{

	STATUS.Index = 0;
	FixedStatusScreen();
	
	Init_Temperature = 0.0;
	Init_CounterModule_Status = NOT_EXIST;

}


int Status_Index;
int CallNum;

#define M_ClkStat_W_UP	0
#define	M_ClkStat_Good	1
#define M_ClkStat_Fail	2
char *strClkStat[]={"W-UP","GOOD","FAIL"};

void UpdateSTATUS()
{
	int i,length;

	char d1,d2;
	int t1,t2,t3,t4;
	double f1,f2,f3,f4;

	double df;
	char message[512];
	
	
	CallNum++;


    STATUS.ID       		= SYSTEM.ID;						// 2

	// Time
	STATUS.Valid			= SYSTEM.TIME.Valid;
    //STATUS.TIME				= SYSTEM.TIME.CUR;
    STATUS.TIME				= SYSTEM.TIME.TOUT;
    STATUS.LEAPSECOND 		= SYSTEM.TIME.LEAPSECOND;
    STATUS.TIMEMODE			= SYSTEM.TIME.TIMEMODE;
    STATUS.LOCALHOUR		= SYSTEM.TIME.LOCALHOUR;
    STATUS.LOCALMINUTE		= SYSTEM.TIME.LOCALMINUTE;
    STATUS.ANTDLY			= SYSTEM.EB.ANTDLY;

    // Position
    STATUS.PosSurvey		= SYSTEM.POSITION.Survey;	/* hold or averaging */
	STATUS.PosNo			= SYSTEM.POSITION.AveragingNo;
	STATUS.PosCounter		= SYSTEM.POSITION.AveragingCounter;
    STATUS.CURRENT			= SYSTEM.POSITION.CURRENT;
    STATUS.AVERAGE			= SYSTEM.POSITION.AVERAGE;
    STATUS.LASTHELD			= SYSTEM.POSITION.LASTHELD;

	// Sat. Track
    STATUS.TRACK			= SYSTEM.TRACK;

    // Operation
    STATUS.OPERATION		= SYSTEM.OPERATION;
    STATUS.RunningTime		= GC.RunningTime;
	STATUS.HoldoverCounter	= CHECKLOCK.HoldoverCounter;
    STATUS.HoldoverRecovery	= RECOVERY.flag;

	// Healeth
    STATUS.DIAG         	= DIAG;                     // 10
    //STATUS.ANTENNA			= SYSTEM.EB.ANTENNA;

	//Control Status
    //STATUS.DAC          	= (int)(CONTROL.RealDAC*100.0); // 4
    //df = CONTROL.TimeOffset*TENMILLION;
    //STATUS.TimeOffset   	= (int)(df*10000); // 4
    //df = CONTROL.AvgTimeOffset*TENMILLION;
    //STATUS.AvgTimeOffset 	= (int)(df*10000); // 4

    // Environment
    STATUS.T_OCXO  			= (short)(TEMPERATURE.CURRENT*10.0); // 2
    STATUS.T_EXT			= (short)(TEMPERATURE.CURRENT*10.0); // 2


    STATUS.TAIL        		= 0xCCDD;						// 2


    // Power up Status message
    if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){
    	STATUS.TIMEMODE = TIMEMODE_GPS;
    	STATUS.LOCALHOUR = 0;
    	STATUS.LOCALMINUTE = 0;
    	STATUS.LEAPSECOND = 0;
    	STATUS.ANTDLY  = 0;
    	STATUS.OPERATION.OPMODE = OPMODE_POWERUP;
    }
    if (SYSTEM.OPERATION.OPMODE != OPMODE_OPERATION) {
    	STATUS.TimeOffset = 0;
    	STATUS.AvgTimeOffset = 0;
    	STATUS.DAC = 0;
    }


   	//-----------------------------------------------------
	//   	STATUS SCREEN
   	//-----------------------------------------------------

   	STATUS.Index = 0;

	
	//if ( SYSTEM.TIME.CUR.SECOND == 30 ) {
	if ( SYSTEM.TIME.TOUT.SECOND == 30 ) {

		STATUS_ASCII[STATUS.Index++] = 0x1B;
		STATUS_ASCII[STATUS.Index++] = '[';
		STATUS_ASCII[STATUS.Index++] = '1';
		STATUS_ASCII[STATUS.Index++] = 'J';
		STATUS_ASCII[STATUS.Index++] = 0x1B;
		STATUS_ASCII[STATUS.Index++] = '[';
		STATUS_ASCII[STATUS.Index++] = '0';
		STATUS_ASCII[STATUS.Index++] = 'J';
	}


   	// Line 1
	if(CGM_P()){
	   	for( i = 0; i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_MainTitle_P[i];	}
	}
	else {
	   	for( i = 0; i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_MainTitle_S[i];	}
	}

	
   	// Line 2 -> Blank

   	// Line 3
   	for( i = 0; i < 69; i++){   		STATUS_ASCII[STATUS.Index++] = SL_SystemTitle[i]; 	}

   	// Line 4
	// Module ID
	switch( SYSTEM.ID ){
		case ModuleA:	SL_System[5+SYSTEM_ID_X] = 'A';	break;
		case ModuleB:	SL_System[5+SYSTEM_ID_X] = 'B';	break;
	}

//	switch( SYSTEM.MODE ){
//		case ModeBTS:	strcpy(message,"BTS");	break;
//		case ModeBSC:	strcpy(message,"BSC");	break;
//		default:	strcpy(message,"   ");	break;
//	}
//	for( i = 0; i < 3; i++){ SL_System[5+SYSTEM_MODEL_X+i] = message[i];}

	// RUN/WARM-UP
	switch( SYSTEM.OPERATION.OPMODE ){
		case OPMODE_POWERUP:			strcpy(message,"POWERUP");				break;
		
		//case OPMODE_WARMUP:				strcpy(message,"WARMUP ");				break;
		//case OPMODE_OPERATION:			strcpy(message,"  RUN  ");				break;
		
		default:
					if (STATUS_Warmup == ON){
						strcpy(message,"WARMUP ");
					} else {
						strcpy(message,"  RUN  ");
					}
					break;
	}
	for( i = 0; i < 7; i++){ SL_System[5+SYSTEM_RUN_X+i] = message[i];}

		// ACTIVE/BACKUP
	if((SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION) && (STATUS_Warmup == OFF)){
		switch( SYSTEM.OPERATION.STATUS ){
			case STATUS_ACTIVE:				strcpy(message,"ACTIVE");				break;
			case STATUS_BLOCK:				strcpy(message,"       ");				break;
			//case STATUS_PRE_STANDBY:		strcpy(message,"       ");				break;
			case STATUS_STANDBY:			strcpy(message,"STANDBY");				break;
			default:						strcpy(message,"       ");				break;
		}
		for( i = 0; i < 7; i++){ SL_System[5+SYSTEM_ACTIVE_X+i] = message[i];}
	}


	t1 = GC.RunningTime/3600;
	t2 = GC.RunningTime-(t1*3600);
	t3 = t2/60;
	t2 = t2-(t3*60);

	sprintf(message,"x[3;66H[%05d:%02d:%02d]",t1,t3,t2);	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

   	for( i = 0 ;i < 54; i++){   		STATUS_ASCII[STATUS.Index++] = SL_System[i];   	}
   	// Line 5
	//sprintf(message,"x[4;63HOCXO    %4.1f (c)",STATUS.T_OCXO/10.0);	message[0] = 0x1B;
	//for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	sprintf(message,"x[4;63HSYSTEM  %4.1f (c)",(float)ts.board_temp);	message[0] = 0x1B;
   	  //sprintf(message,"x[4;63HSYSTEM  N/A  (c)");	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

//	//sprintf(message,"x[5;63HOCXO    N/A  (c)");	message[0] = 0x1B;
//	sprintf(message,"x[5;63HOCXO    %4.1f (c)",(float)TEMPERATURE.CURRENT);	message[0] = 0x1B;
//	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	//sprintf(message,"x[5;63HSYSTEM  %4.1f (c)",STATUS.T_EXT/10.0);	message[0] = 0x1B;
	//for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

   	// Line 6 -> Blank

   	// Line 7
   	// CONTROL TIME d Valid
	switch(SYSTEM.OPERATION.OPMODE){
		case	OPMODE_POWERUP:
		case	OPMODE_WARMUP:
					if( SYSTEM.TIME.Valid == NO ) {
   						for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_3[i];   	}
   					} else {
	   					for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_2[i];   	}
	   				}
   					break;
		case	OPMODE_OPERATION:
					if(SYSTEM.OPERATION.LOCKMODE==LOCKED && RECOVERY.flag == ON){
						for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_5[i];   	}
					}
					else {
   						if( SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN ){
   							if(CONTROL.Run_Mode == FREE_RUN){
   								for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_6[i];   	}
   							}
   							else {
   								for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_4[i];   	}
   							}
   						}
   						else {
	   						for( i = 0;i < 83; i++){   		STATUS_ASCII[STATUS.Index++] = SL_ControlTimeTitle_1[i];   	}
   						}
   					}
   					break;
	}

	// Line 8 -> Blank

	// Line 9~13 Control & Time


	// Control
	switch(SYSTEM.OPERATION.OPMODE){
		case	OPMODE_OPERATION:
					f1 = CONTROL.RealDAC;
					if( SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN ){
						f2 = f3 = df = 0;
					} else {
						f2 = CONTROL.TimeOffset*BILLION;
						f3 = CONTROL.AvgTimeOffset*BILLION;
						df = CONTROL.Frequency;
					}
					break;
		case	OPMODE_POWERUP:
		case	OPMODE_WARMUP:
					f1 = f2 = f3 = 0;
					df = 0;
					break;
	}


	if( SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN ){

		//t1 = CONTROL.HoldoverDuration/3600;
		//t2 = CONTROL.HoldoverDuration-(t1*3600);

		if( CONTROL.Run_Mode != FREE_RUN ){

			t1 = CHECKLOCK.HoldoverCounter/3600;
			t2 = CHECKLOCK.HoldoverCounter-(t1*3600);
			t3 = t2/60;
			t2 = t2-(t3*60);

			sprintf(message,"x[9;4HHoldover Duration.........[%03d:%02d:%02d]",t1,t3,t2);	message[0] = 0x1B;
		}
	}
	else {

		// 1. ALMANAC (Power-up)
		// 2. 3D-FIX (Survey)
		if ( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP && CONTROL.WaitToALMANACCounter > 30 ) {

			strcpy(message,"x[9;4HReceiving ALMANAC data...             ");	message[0] = 0x1B;
		}
		else if ( SYSTEM.OPERATION.OPMODE == OPMODE_WARMUP && CONTROL.WaitToControlCounter > 10 ) {

			strcpy(message,"x[9;4HWaiting 3D-FIX...                    ");	message[0] = 0x1B;
		}
		else {
			//strcpy(message,"x[9;4H                                     ");	message[0] = 0x1B;
			message[0] = (char)0;
		}
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	if(CGM_P()){
		sprintf(message,"x[9;4HDAC           %8.2f",(f1+10000)*3.2768);	message[0] = 0x1B;
	}
	else {
		sprintf(message,"x[9;4HDAC           %8.2f",f1);	message[0] = 0x1B;
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	if(IsRefLocked()){
		sprintf(message,"x[10;4HTime Offset  %7.1fns",f2);	message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
		sprintf(message,"x[11;4HAvg Offset   %7.1fns",f3);	message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
		sprintf(message,"x[12;4HFrequency   %+5.3E",df);	message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	}
	else {
		strcpy(message, "x[10;4HTime Offset        N/A"\
						"x[11;4HAvg Offset         N/A"\
						"x[12;4HFrequency          N/A");	message[0] = 0x1B;message[29] = 0x1B;message[58] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	}

	// Trace ��� �����ؼ� ����� ���̺��� �� ã����..
	//if (TRACE.MonitorTableIndex >= TraceMonitorTable_SIZE ) {
	if(SYSTEM.OPERATION.CTRLMODE == CTRL_LOCKEDtoMASTER){
    	t1 = (int)TRACE.TimeOffset;
    }
    else {
    	t1 = 99999;
    }

	//sprintf(message,"x[13;4HTrace Offset %7dns",t1);	message[0] = 0x1B;
//	sprintf(message,"%c[13;4HTrace Offset       N/A ",0x1B);	//message[0] = 0x1B;
//	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	if( SYSTEM.TIME.Valid ) {
		sprintf(message,"x[9;48H%.4d/%.2d/%.2d      %.2d:%.2d:%.2d",
			SYSTEM.TIME.TOUT.YEAR, SYSTEM.TIME.TOUT.MONTH, SYSTEM.TIME.TOUT.DAY,
			SYSTEM.TIME.TOUT.HOUR, SYSTEM.TIME.TOUT.MINUTE, SYSTEM.TIME.TOUT.SECOND);	
		message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){
			STATUS_ASCII[STATUS.Index++] = message[i];
		}
	}

	switch(STATUS.TIMEMODE){
		case TIMEMODE_GPS:	strcpy(message,"x[10;48HTime Mode       GPS");message[0] = 0x1B; break;
		case TIMEMODE_UTC:	strcpy(message,"x[10;48HTime Mode       UTC");message[0] = 0x1B; break;
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	sprintf(message,"x[11;48HLocal Time      %.2dH %.2dM",STATUS.LOCALHOUR,STATUS.LOCALMINUTE);					message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	// Timing Antenna  d�� ǥ�?
	if(SYSTEM.EB.STATUS == EB_STATUS_RUN){
		sprintf(message,"x[12;48HLeap Second     %.2d",STATUS.LEAPSECOND);					message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
		sprintf(message,"x[13;48HAntenna Delay   %ldns",STATUS.ANTDLY);					message[0] = 0x1B;
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	}
	// Line 14, 15, 16 -> Blank

	// Line 17
   	//for( i = 0;i < 84; i++){   		STATUS_ASCII[STATUS.Index++] = SL_PositionSatTitle[i];   	}
	STATUS_ASCII[STATUS.Index++] = 0x1B;
	if( CONFIG.RcvType==1){
		for( i = 1;i < strlen(SL_PositionSatTitle); i++){   		STATUS_ASCII[STATUS.Index++] = SL_PositionSatTitle[i];   	}
	}
	else{
		for( i = 1;i < strlen(SL_PositionSatTitl2); i++){   		STATUS_ASCII[STATUS.Index++] = SL_PositionSatTitl2[i];   	}
	}

   	// Line 18 ~ 22 (Position & Satellites)

	// Position
	switch(SYSTEM.POSITION.PositionMode){
		case POSITION_MODE_KNOWN:
					strcpy(message, "x[17;4HMODE    Known            ");
					break;

		case POSITION_MODE_AVERAGING:
					if(SYSTEM.EB.STATUS == EB_STATUS_RUN){
						sprintf(message, "x[17;4HMODE    Averaging   %4.1f%%",100.0*(float)SYSTEM.POSITION.AveragingCounter/(float)SYSTEM.POSITION.AveragingNo);
					}
					else {
						strcpy(message, "x[17;4HMODE    Averaging        ");
					}
					break;

		case POSITION_MODE_DYNAMIC:
					strcpy(message,"x[17;4HMODE    Dynamic          ");
					break;
		default:
					sprintf(message,"x[17;4HMODE                     ");
					break;
	}

	message[0] = 0x1B;
	if( CONFIG.RcvType==1 )
	{
		for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}
	}

	t3 = SYSTEM.POSITION.CURRENT.LAT/3600000;
	t4 = (SYSTEM.POSITION.CURRENT.LAT%3600000)/60000;
	f2 = (float)((SYSTEM.POSITION.CURRENT.LAT%3600000)%60000)/1000.0;

   	if( SYSTEM.POSITION.CURRENT.LAT >= 0 ) {	d2 = 'N';		}
	else {		d2 = 'S';	t3 = -t3;	t4 = -t4;	f2 = -f2;	}

	sprintf(message,"x[19;4HLAT.   %c %3d,%2d,%6.3f",d2,t3,t4,f2);
	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	t3 = SYSTEM.POSITION.CURRENT.LON/3600000;
	t4 = (SYSTEM.POSITION.CURRENT.LON%3600000)/60000;
	f2 = (float)((SYSTEM.POSITION.CURRENT.LON%3600000)%60000)/1000.0;

   	if( SYSTEM.POSITION.CURRENT.LON >= 0 ) {	d2 = 'E';		}
	else {		d2 = 'W';	t3 = -t3;	t4 = -t4;	f2 = -f2;	}

	sprintf(message,"x[20;4HLON.   %c %3d,%2d,%6.3f",d2,t3,t4,f2);
	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}


	f2 = (float)(SYSTEM.POSITION.CURRENT.HEI/100.0);

	sprintf(message,"x[21;4HELV.          %+7.2fm",f2);
	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	//int d_offset= (CONFIG.RcvType?48:48-3*4);
	//int n_prn	= (CONFIG.RcvType?8:12);
	int d_offset= ((CONFIG.RcvType==1)?48:48-3*2);
	int n_prn	= ((CONFIG.RcvType==1)?8:10);
	// Satellite tracking status

	sprintf(message,"%c[17;%dHDOP%c[17;73H%3.1f",0x1B,d_offset,0x1B,SYSTEM.TRACK.DOP/10.0);
	//message[0] = 0x1B; message[11] = 0x1B;

//	sprintf(message,"x[17;38HHDOP %5.1f     VDOP %5.1f     PDOP %5.1f",SYSTEM.TRACK.HDOP,SYSTEM.TRACK.VDOP,SYSTEM.TRACK.PDOP);
//	message[0] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	sprintf(message,"%c[18;%dHVisible%c[18;74H%2d",0x1B,d_offset,0x1B,SYSTEM.TRACK.VISIBLE);
	//message[0] = 0x1B; message[15] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	sprintf(message,"%c[19;%dHTracking%c[19;74H%2d",0x1B,d_offset,0x1B,SYSTEM.TRACK.TRACKED);
	//message[0] = 0x1B; message[16] = 0x1B;
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	sprintf(message,"%c[20;%dHPRN:",0x1B,d_offset);
	for(i=0; i<n_prn; i++){
		sprintf(MESG," %.2d",SYSTEM.TRACK.SAT[i].ID);
		strcat(message,MESG);
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	sprintf(message,"%c[21;%dHS/S:",0x1B,d_offset);
	for(i=0; i<n_prn; i++){
		sprintf(MESG," %.2d",SYSTEM.TRACK.SAT[i].SS);
		strcat(message,MESG);
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}

	char used;
	sprintf(message,"%c[22;%dHUSE:",0x1B,d_offset);
	for(i=0; i<n_prn; i++){
		if( SYSTEM.TRACK.SAT[i].MODE == 8) used = '*';
		else used = ' ';
		sprintf(MESG,"  %c",used);
		strcat(message,MESG);
	}
	for( i = 0, length = strlen(message); i < length; i++){ STATUS_ASCII[STATUS.Index++] = message[i];}


   	// Line 23 -> Blank

	
	//========================================= HEALTH =============================================
   	// Line 24
   	for( i = 0;i < 84; i++){   		STATUS_ASCII[STATUS.Index++] = SL_HealthTitle[i];   	}

	if(DIAG.DUAL_RX == FAIL) {strcpy(message,"COMM");}
	else if(DIAG.TRACE_10M_COUNTER == FAIL) {strcpy(message,"10M ");}
	//else if(CounterModule.EXIST == NO) {strcpy(message,"NULL");}
	else if(CounterModule.EXIST == NO) {strcpy(message," NA ");}
	//hmlee - couterModule exist down here
	else if(DIAG.DUAL_TEST != OK) {strcpy(message,"CHNN");}
	else {strcpy(message," OK ");}
	
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_DUAL_X+i] = message[i];}
	
   	// Line 25
	//if(!SYSTEM.OPERATION.SELFTEST) {	strcpy(message," OK ");	}
	//else{								strcpy(message,"FAIL");	}
	//for( i = 0; i < 4; i++){ SL_Health[HEALTH_SELFTEST_X+i] = message[i];}
	if(IsSigOn()) {	strcpy(message," ON ");	}
	else{strcpy(message,"OFF ");	}
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_SIG_X+i] = message[i];}

	switch( DIAG.POWER ){
		case OK: 	strcpy(message," OK ");				break;
		case FAIL:	strcpy(message,"FAIL");				break;
	}
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_POWER_X+i] = message[i];}

	switch( DIAG.EFC ){
		case OK: 	strcpy(message," OK ");				break;
		case FAIL:	strcpy(message,"FAIL");				break;
	}
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_EFC_X+i] = message[i];}

	//switch( DIAG.RTC ){
	//	case OK: 	strcpy(message," OK ");				break;
	//	case FAIL:	strcpy(message,"FAIL");				break;
	//}
	//if(DIAG.i_exist == NO){strcpy(message,"OPEN");}
	//else{strcpy(message," NA ");}

	//for( i = 0; i < 4; i++){ SL_Health[HEALTH_RTC_X+i] = message[i];}
	if( STATUS_Warmup == ON ) 	t1= M_ClkStat_W_UP;
	else {
		if( DIAG._10MHz != FAIL )	t1=M_ClkStat_Good;
		else 						t1=M_ClkStat_Fail;
	}
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_RTC_X+i] = strClkStat[t1][i];}

	// Reference
	// RTC, E1/8K, Timing Antenna
	//if(REFERENCE_MODE == EB_STATUS_RUN){
	//	strcpy(message," GPS /");
	//	if( SYSTEM.EB.ANTENNA == OK){
	//		strcat(message,"   OK  ");
	//	}
	//	else if ( SYSTEM.EB.ANTENNA == ANTENNA_SHORTED){
	//		strcat(message,"  SHRT ");
	//	}
	//	else if( SYSTEM.EB.ANTENNA == ANTENNA_OPENED){
	//		strcat(message,"  OPEN ");
	//	}
	//	else {
	//		strcat(message,"   -   ");
	//	}
	//}
	//else if(REFERENCE_MODE == E1_STATUS_RUN){
	//	strcpy(message,"  E1 /");
	//	if( SYSTEM.E1.STATUS == E1_STATUS_RUN){
	//		strcat(message,"   IN  ");
	//	}
	//	else{
	//		strcat(message,"  OUT  ");
	//	}
	//}
	//else {
	//	strcpy(message,"  -  /");
	//	strcat(message,"   -   ");					   
	//}
	//for( i = 0; i < 13; i++){ SL_Health[HEALTH_REFERENCE_X+i] = message[i];}
	
	if(CONFIG.AntCheckFlag){
		switch( SYSTEM.EB.ANTENNA ){
			case OK:					strcpy(message," OK ");				break;
			case ANTENNA_SHORTED:		strcpy(message,"SHRT");				break;
			case ANTENNA_OPENED:		strcpy(message,"OPEN");				break;
			default:					strcpy(message,"    ");				break;
		}
	}
	else{
		strcpy(message," NA ");
	}
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_ANT_X+i] = message[i];}
	
//	if( QueryFANOPEN() )		{strcpy(message,"OPEN");}
//	else if( QueryFANError() )	{strcpy(message,"FAIL");}
//	if( QueryFANALM1()	&&	QueryFANALM2() ){ strcpy(message,"FAIL"); }
//	else if( QueryFANALM1()	)				{ strcpy(message,"FLT1"); }
//	else if( QueryFANALM2()	)				{ strcpy(message,"FLT2"); }
//	else									{ strcpy(message," OK "); }
	//strcpy(message," NA ");
	//strcpy(message,"FAIL");
	strcpy(message," NA ");
	for( i = 0; i < 4; i++){ SL_Health[HEALTH_FAN_X+i] = message[i];}
	

//	switch( SYSTEM.EB.ANTENNA ){
//		case OK:  				strcpy(message," OK ");				break;
//		case ANTENNA_OPENED:	strcpy(message,"OPEN");				break;
//		case ANTENNA_SHORTED:	strcpy(message,"SHRT");				break;
//	}
//	for( i = 0; i < 4; i++){ SL_Health[6+HEALTH_ANTENNA_X+i] = message[i];}
//	if( SYSTEM.EB.SELFTEST != FAIL && DIAG.EB != FAIL ){
//		strcpy(message," OK ");
//	}
//	else {
//		strcpy(message,"FAIL");
//	}
//	for( i = 0; i < 4; i++){ SL_Health[6+HEALTH_EB_X+i] = message[i];}

   	//for( i = 0;i < 83; i++){   STATUS_ASCII[STATUS.Index++] = SL_Health[i];		}
   	for( i = 0;i < 84; i++){   STATUS_ASCII[STATUS.Index++] = SL_Health[i];		}
	
	Status_Index = STATUS.Index;
	
	//write(STDOUT_FILENO, STATUS_ASCII, STATUS.Index);
	//printf("CallNum:%d\n",CallNum);
	
	//printf( STATUS_ASCII );
	//printf("CallNum:%d\n",CallNum);
	
	//write(0, "0> hello world\n", strlen("hello world\n"));
	//write(STDERR_FILENO, "stderr> hello world\n", strlen("hello world\n"));
	//write(STDOUT_FILENO, "stdout> hello world\n", strlen("hello world\n"));
	//printf("IDX: %d, STRLEN: %d\n", STATUS.Index, strlen(STATUS_ASCII));
	
	
}

void FixedStatusScreen(void)
{
	int i;

	// line 1
	SL_MainTitle_P[0] = 0x1B;
	SL_MainTitle_S[0] = 0x1B;
	// line 3
	SL_SystemTitle[0] = 0x1B;
	// line 4
	SL_System[0] = 0x1B;
	// line 7
	SL_ControlTimeTitle_1[0] = 0x1B;
	SL_ControlTimeTitle_2[0] = 0x1B;
	SL_ControlTimeTitle_3[0] = 0x1B;
	SL_ControlTimeTitle_4[0] = 0x1B;
	SL_ControlTimeTitle_5[0] = 0x1B;
	SL_ControlTimeTitle_6[0] = 0x1B;
	// line 17
	SL_PositionSatTitle[0] = 0x1B;
	// line 24
	SL_HealthTitle[0] = 0x1B;
	// line 25
	SL_Health[0] = 0x1B;
}


/*----------------------------------------------------------------------
 File Name : TODIO.C
 Last Updated : 98-02-12 9:40
 Function Name : STATUS_OUT
 Description : TATUS
 Parameters : port
----------------------------------------------------------------------*/
void STATUS_OUT(int port, char mode )
{
	switch(mode){

		case	ASCII:
    				//kang TransmitnMessage((unsigned char *)(&STATUS_ASCII),STATUS.Index,port);
					
					write(STDOUT_FILENO, STATUS_ASCII, Status_Index);
					udp_local_cmd_send_size(STATUS_ASCII, Status_Index);
					//TransmitnMessage((unsigned char *)(&STATUS_ASCII),STATUS.Index,port);
					
					//printf("CallNum:%d, Status_Index:%d\n",CallNum, Status_Index);
					
					//printf( STATUS_ASCII );
					//printf("CallNum:%d, Status_Index:%d\n",CallNum, Status_Index);
					
					break;
		case	BINARY:
    				TransmitnMessage((unsigned char *)(&STATUS),sizeof(STATUS_),port);
					break;
		case 	TEXT:
					status_text_out();
					break;
	}
}



void status_text_out(void)
{
	int t1,t2,t3;

	char message[200];


// 1. Version
	if(CGM_P()){
		sprintf(message,"STAT,P,%d,%s,%s,",SYSTEM.ID,FirmWare_Version,FPGA_Version);
	}
	else {
		sprintf(message,"STAT,S,%d,%s,%s,",SYSTEM.ID,FirmWare_Version,FPGA_Version);
	}
	TransmitMessage((BYTE*)message,PORT_CONTROL);


	t1 = GC.RunningTime/3600;
	t2 = GC.RunningTime-(t1*3600);
	t3 = t2/60;
	t2 = t2-(t3*60);

	sprintf(message,"%.4d%.2d%.2d%.2d%.2d%.2d,%.5d%.2d%.2d,",
		SYSTEM.TIME.TOUT.YEAR, SYSTEM.TIME.TOUT.MONTH, SYSTEM.TIME.TOUT.DAY,
		SYSTEM.TIME.TOUT.HOUR, SYSTEM.TIME.TOUT.MINUTE, SYSTEM.TIME.TOUT.SECOND,
		t1,t3,t2);
	TransmitMessage((BYTE*)message,PORT_CONTROL);


	sprintf(message,"%s,%.2e,",CONFIG.OCXO_ID,CONFIG.GAIN);
	TransmitMessage((BYTE*)message,PORT_CONTROL);



// 2.
	sprintf(message,"%.1f,%.1f,%.1f,",fPlus5V,fPlusVcc,fMinusVee);
	TransmitMessage((BYTE*)message,PORT_CONTROL);

// 3. Alarm �?
	sprintf(message,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,",
					DIAG.SYSTEM,DIAG.EB,DIAG.OSC,DIAG._10MHz,DIAG._10MHz_GoodCounter,DIAG.OCXO,DIAG.Watch1PPSCounter,
					DIAG.RB,DIAG.POWER,DIAG.EFC,DIAG.RTC,DIAG.dDAC);
	TransmitMessage((BYTE*)message,PORT_CONTROL);

// 4.

	message[1] = ',';
	message[3] = ',';

	switch(SYSTEM.OPERATION.STATUS){
		case STATUS_ACTIVE:			message[0] = 'A';	break;
		case STATUS_STANDBY:		message[0] = 'S';	break;
		//case STATUS_PRE_STANDBY:	message[0] = 'P';	break;
		case STATUS_BLOCK:			message[0] = 'B';	break;
		default:					message[0] = 'X';	break;
	}

	if (CounterModule.EXIST == NO) {
		message[2] = 'N';
	}
	else{
		switch(CounterModule.STATUS){
			case STATUS_ACTIVE:		message[2] = 'A';	break;
			case STATUS_STANDBY:	message[2] = 'S';	break;
			//case STATUS_PRE_STANDBY:message[2] = 'P';	break;
			case STATUS_BLOCK:		message[2] = 'B';	break;
			default:				message[2] = 'X';	break;
		}
	}
	TransmitnMessage((BYTE*)message,4,PORT_CONTROL);

// 5. DAC
	sprintf(message,"%d,%d,",(int)CONFIG.DAC,(int)CONTROL.RealDAC);
	TransmitMessage((BYTE*)message,PORT_CONTROL);

// 6. 8K
	message[1] = ',';
	switch(SYSTEM.E1.STATUS){
		case E1_STATUS_RUN:		message[0] = 'R';	break;
		case E1_STATUS_SET:		message[0] = 'S';	break;
		case E1_STATUS_NONE:	message[0] = 'N';	break;
		default:				message[0] = 'X';	break;
	}
	TransmitnMessage((BYTE*)message,2,PORT_CONTROL);

// 7. Timing Antenna
//	sprintf(message,"X,%d,%d,%.1f,%.1f,%.1f,",SYSTEM.TRACK.VISIBLE,SYSTEM.TRACK.TRACKED,SYSTEM.TRACK.HDOP,SYSTEM.TRACK.VDOP,SYSTEM.TRACK.PDOP);
	sprintf(message,"X,%d,%d,%.1f,%.1f,%.1f,",SYSTEM.TRACK.VISIBLE ,SYSTEM.TRACK.TRACKED ,(double)0 , (double)0 ,(double)((double)SYSTEM.TRACK.DOP/10.));
	switch(SYSTEM.EB.STATUS){
		case EB_STATUS_RUN:		message[0] = 'R';	break;
		case EB_STATUS_SELFTEST:message[0] = 'T';	break;
		case EB_STATUS_SET:		message[0] = 'S';	break;
		case EB_STATUS_NONE:	message[0] = 'N';	break;
		default:				message[0] = 'X';	break;
	}
	TransmitMessage((BYTE*)message,PORT_CONTROL);

// 8. Position

	strcpy(message,GetPositionString(SYSTEM.POSITION.AVERAGE));
	TransmitMessage((BYTE*)message,PORT_CONTROL);

	message[0] = ',';
	switch(SYSTEM.POSITION.PositionMode){
		case POSITION_MODE_KNOWN:		message[1] = 'K';	break;
		case POSITION_MODE_AVERAGING:	message[1] = 'P';	break;
		case POSITION_MODE_DYNAMIC:		message[1] = 'D';	break;
	}
	message[2] = (char)0;
	message[3] = 0x0D;
	message[4] = 0x0A;
	TransmitnMessage((BYTE*)message,5,PORT_CONTROL);

}

char *str_onoff[2]={"OFF", "ON "};
char *str_ok[2]={"NOT", "OK "};
char *str_side[2]={ "A", "B"};
char *str_dualEvnt[7]={"NONE        ","BLK2ACT&STBY","ACT_2_BLCK  ","STBY_2_BLCK ","ACT_2_STBY  ","STBY_2_ACT  ","preSTBY2STBY"};
static char string1[200],string2[200],string3[200],string4[200];

void DEBUG_OUT()
{
	int t1,t2,t3;
	
	int i;
	unsigned short* addr;
	unsigned char data;

	return ; ///////////////////////////
	
	if( GC.RunningTime%60 == 0 ){
//		sprintf(MESG,  "%c[1J%c[0J" ,0x1B,0x1B );
//		TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	}
	//SYSTEM.ID = 0;
//	printf("%c[1;1HSLOT:%s,%d /" ,0x1B,str_side[0],(int)SYSTEM.ID);// (int)SYSTEM.ID ] );
//	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	//sprintf(MESG,  "%c[1;1HSLOT:%s,%d /" ,0x1B,str_side[0],(int)SYSTEM.ID);// (int)SYSTEM.ID ] );
//	sprintf(MESG,  "SLOT:%s,%d /" ,str_side[0],(int)SYSTEM.ID);// (int)SYSTEM.ID ] );
//	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	printf("SLOT:%s,%d /" ,str_side[0],(int)SYSTEM.ID);// (int)SYSTEM.ID ] );

	return ; ///////////////////////////

	
	t1 = GC.RunningTime/3600;
	t2 = GC.RunningTime-(t1*3600);
	t3 = t2/60;
	t2 = t2-(t3*60);
	string1[0] = (char)0;
	switch(SYSTEM.TIME.TIMEMODE){
		case TIMEMODE_GPS:
			strcpy(string1,"GPS");
			break;
		case TIMEMODE_UTC:
			strcpy(string1,"UTC");
			break;
		default:
			strcpy(string1,"   ");
			break;
	}

	return ; ///////////////////////////

	//Line 2
	sprintf(MESG,	"Run[%05d:%02d:%02d]"
					" [%.4d/%.2d/%.2d "
					"%.2d:%.2d:%.2d] "
					"Mode:[%s],L:%2d, ACC:%10d,\n",
					t1,t3,t2,
					SYSTEM.TIME.TOUT.YEAR, SYSTEM.TIME.TOUT.MONTH,  SYSTEM.TIME.TOUT.DAY,
					SYSTEM.TIME.TOUT.HOUR, SYSTEM.TIME.TOUT.MINUTE, SYSTEM.TIME.TOUT.SECOND,
					string1, SYSTEM.TIME.LEAPSECOND, GC.ACCsecond );
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);

	
	//Line : GPS Detail
	sprintf(MESG,	"DT: %.2X,V: %3d,T: %3d,ST: %.2X ",
					SYSTEM.TRACK.DOPTYPE,SYSTEM.TRACK.VISIBLE,SYSTEM.TRACK.TRACKED,SYSTEM.TRACK.STATUS);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	return ; ///////////////////////////
	
	//Line 3
	//sprintf(MESG, 	"/ TEMPERATURE: :%6.2f,ave:%6.2f ",
	//				(float)TEMPERATURE.CURRENT,(float)TEMPERATURE.AVERAGE);
//	sprintf(MESG, 	"/ TEMP:%6.2f,%6.2f,ave:%6.2f ",
//					(float)ts.board_temp,
//					(float)TEMPERATURE.CURRENT,(float)TEMPERATURE.AVERAGE);
	sprintf(MESG, 	"/ TEMP:%6.2f ",(float)ts.board_temp );
			TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	sprintf(MESG,"/ +5V:%.2f,VCC:%.2f,VEE:%.2f \n\n",fPlus5V,fPlusVcc,fMinusVee);
 	TransmitMessage((BYTE*)MESG,PORT_CONTROL);

	sprintf(MESG, "============================================================================\n");
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	return ; ///////////////////////////
	//
	string1[0] = (char)0;
	switch(REFERENCE_MODE){
		case EB_STATUS_RUN:
			strcpy(string1,"GPS");
			break;
		case LMU_STATUS_RUN:
			strcpy(string1,"LMU");
			break;
		case E1_STATUS_RUN:
			strcpy(string1,"8K1");
			break;
		case E2_STATUS_RUN:
			strcpy(string1,"8K2");
			break;		
		default:
			strcpy(string1,"---");
			break;
	}
	
	
	string2[0] = (char)0;
	switch(SYSTEM.EB.STATUS){
		case EB_STATUS_NONE:
			strcpy(string2,"  NONE  ");
			break;
		case EB_STATUS_SELFTEST:
			strcpy(string2,"SELFTEST");
			break;
		case EB_STATUS_ALMANAC:
			strcpy(string2," ALMANAC");
			break;
		case EB_STATUS_SET:
			strcpy(string2,"   SET  ");
			break;
		case EB_STATUS_RUN:
			strcpy(string2,"   RUN  ");
			break;
		default:
			strcpy(string2,"        ");
			break;
	}
	
	sprintf(MESG, "Ref.Status. Ref.Mode[%s],GPS[%s] ,ledCnt:%2d syncCnt:%d\n",string1,string2,led_blk_cnt,led_sync_retain_cnt);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);

	//Line : Ctrl Mode & OPERATION �?Readable.
	string1[0] = (char)0;
	switch(SYSTEM.OPERATION.CTRLMODE){
		case CTRL_LOCKEDtoGPS:
			strcpy(string1,"  GPS ");
			break;
		case CTRL_LOCKEDtoMASTER:
			strcpy(string1," TRACE");
			break;
		default:
			strcpy(string1,"     ");
			break;
	}
	
	string2[0] = (char)0;
	switch(SYSTEM.OPERATION.OPMODE){
		case OPMODE_POWERUP:
			strcpy(string2,"POWERUP");
			break;
		case OPMODE_WARMUP:
			strcpy(string2," WARMUP");
			break;
		case OPMODE_OPERATION:
			strcpy(string2,"  RUN  ");
			break;
		default:
			strcpy(string1,"       ");
			break;
	}
	
	//Lock Mode
	string3[0] = (char)0;
	switch(SYSTEM.OPERATION.LOCKMODE){
		case LOCKED:
			strcpy(string3,"LOCKED");
			break;
		case HOLDOVER:
			strcpy(string3," HOLD ");
			break;
		case HOLDOVER_OVER10MIN:
			strcpy(string3,"HOLD10");
			break;
		case UNLOCKED:
			strcpy(string3,"UNLOCK");
			break;		
		default:
			strcpy(string3,"      ");
			break;
	}
	
	sprintf(MESG, "Ctrl.Mode[%s],OP.Mode[%s],LockMode[%s], FirstLock[%s]/%5d \n"
					,string1,string2,string3,str_ok[FIRST_LOCK], QuickStartCounter);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	string1[0] = (char)0;
	switch(SYSTEM.OPERATION.STATUS){
		case STATUS_ACTIVE:			strcpy(string1,"   ACTIVE  ");		break;
		case STATUS_STANDBY:		strcpy(string1,"  STANDBY  ");		break;
		case STATUS_PRE_STANDBY:	strcpy(string1,"PRE_STANDBY");		break;
		case STATUS_BLOCK:			strcpy(string1,"   BLOCK   ");		break;
		case STATUS_UNKNOWN:		strcpy(string1,"  UNKNOWN  ");		break;
	}
	
	string2[0] = (char)0;
	switch(CounterModule.STATUS){
		case NOT_EXIST:				strcpy(string2,"    NONE   ");		break;
		case STATUS_ACTIVE:			strcpy(string2,"   ACTIVE  ");		break;
		case STATUS_STANDBY:		strcpy(string2,"  STANDBY  ");		break;
		case STATUS_PRE_STANDBY:	strcpy(string2,"PRE_STANDBY");		break;
		case STATUS_BLOCK:			strcpy(string2,"   BLOCK   ");		break;
		case STATUS_UNKNOWN:		strcpy(string2,"  UNKNOWN  ");		break;
	}

	string4[0] = (char)0;
	switch(DIAG.DUAL_TEST){
		case UNDEFINED:				strcpy(string4,"   ");		break;
		case OK:					strcpy(string4,"OK ");		break;
		case FAIL:					strcpy(string4,"NOT");		break;
	}
	
	sprintf(MESG,	"DualStatus[%s/%s]/[%d,%d], sigOn:%s ,Ch:%s[0x%x/0x%x]\n",
		string1,string2, psda.my_priority_value, psda.your_priority_value,str_onoff[ IsSigOn()],
		string4,dual_test_last_step,dual_test_last_val
	);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	string1[0] = (char)0;
	switch(DIAG.DUAL_RX){
		case UNDEFINED:				strcpy(string1,"   ");		break;
		case OK:					strcpy(string1,"OK ");		break;
		case FAIL:					strcpy(string1,"NOT");		break;
	}
	
	string2[0] = (char)0;
	switch(DIAG.TRACE_10M_COUNTER){
		case UNDEFINED:				strcpy(string2,"   ");		break;
		case OK:					strcpy(string2,"OK ");		break;
		case FAIL:					strcpy(string2,"NOT");		break;
	}
	
	sprintf(MESG,	"Diag:%s Open:%s(%3d) / %s" " PWR:%s ,OSC:%s ,Comm:%s ,T10M:%s" "\n"
		,str_ok[ (int)DIAG.SYSTEM ] ,str_ok[ (DIAG.i_exist==YES) ], DIAG.DELETION_COUNTER ,str_dualEvnt[(int)MyModule.StatusEvent]
		,str_ok[ (int)DIAG.POWER ],str_ok[ (int)DIAG.OSC ]
		,string1,string2
		);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	sprintf(MESG, "============================================================================\n");
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	//Line 8: Lock
	sprintf(MESG, 	"Master. Interval:%4d,STEP:%4d,K_I:%2d,RO:%13.2f,RD:%13.2f\n"
					//"        R:%9.2f,M:%9.2f,OCXO:%6.2f,%s,HEI:%6d,\n",
					"        R:%9.2f,M:%9.2f,m:%9.2f,OCXO:%6.2f,%s,HEI:%6d,\n",
					CONTROL.IntervalCounter,CONTROL.StepCounter,KInterval,(float)RAW_OFFSET, (float)RAW_DIFFERENCE,
					(float)CONTROL.RealDAC, (float)MeanDAC, (float)ave_dac.recent, (float)OCXO_ID.TDAC,OCXO_ID.id_str,	(unsigned)SYSTEM.POSITION.CURRENT.HEI
					//(float)CONTROL.RealDAC, (float)MeanDAC, (float)OCXO_ID.TDAC,OCXO_ID.id_str,	SYSTEM.POSITION.CURRENT.HEI
			);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
		
	//Line
	sprintf(MESG,	"Holdover. STEP:%10d,Dur:%10d,ARef:%13.2f,"
					"Ph:%3d,Calc:%3d,\n",
					HoldoverCONTROL.StepCounter, CONTROL.HoldoverDuration, HoldoverCONTROL.AgingReference,
					HoldoverCONTROL.Phase,HoldoverCalc.flag);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	t1 = ReadMainCounterAll();
	t2 = ReadSynthCounter();
	t2+=esync_debug;
	
	//Line 11:Counter �?
	sprintf(MESG,	"10M Counter.%10d,%10d\n",t1,t2);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	sprintf(MESG, "============================================================================\n");
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	//t1 = ssDetailTable[gTraceTableIndex]/10;
	
	//Line 9: Trace
	sprintf(MESG,	"TRACE. CtrlCnt:%6d,Turn:%4d,Dly:%3d,M:%9.2f,Diff:%10d,ns \n"
					"       index:%6d, matchCnt:%3d , ok:%s " "/ [%2d,%2d]"
					" \n"
					,TRACE.ControlCounter,TRACE.control_turn,TRACE.DelayCounter,(float)TRACE.MeanDAC,TRACE.TimeOffset
					,TRACE.MonitorTableIndex, iTraceMatchCnt,  str_ok[(int)LOCKEDtoMASTER_SPEC_IN]
					,CONFIG.delay_value,(int)TRACE.detailSense
			);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	t1 = ReadTraceCounterFPGA();
	
	//Line 11:Counter �?
	sprintf(MESG,	"10M Counter.%10d\n",t1);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	sprintf(MESG, "============================================================================\n");
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);

	//Line 4:Reference
	sprintf(MESG,	"LockRef.:%.3d,LockMode:%.3d,RefMode:%.3d\n"
					"G:%.3d,LMU:%.3d,E:%.3d,%.3d,%d\n\n",
					LOCK_REFERENCE, SYSTEM.OPERATION.LOCKMODE, REFERENCE_MODE,
					SYSTEM.EB.STATUS, lmu.STATUS, SYSTEM.E1.STATUS, SYSTEM.E2.STATUS, MyReference);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	
	//Line 5:Startupܰ Warmupܰ
	sprintf(MESG,	"OPERATION:%d,Startup:%d,%d,%3d,Warmup:%d\n",
					SYSTEM.OPERATION.OPMODE,
					startup.MAINSTEP, startup.self_test_step, startup.IntervalCounter,
					CONTROL.SURVEYSTEP);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	
	//Line 6:OCXO Wampup
	sprintf(MESG,	"OCXO. PRT:%10d,PPT:%10d,"
					"MWT:%4d,WTC:%4d,T:%.2f,C_ST:%d\n"
					"OCXO Warmup:%13.1f,%4d,\n",
					prev_running_time,prev_power_time,
					Module_Warmup_Time,WarmupTimeCounter,Init_Temperature,Init_CounterModule_Status,
					CONTROL.warmup_average,CONTROL.warmup_counter
					);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
		
	//Line 7
	sprintf(MESG,	"CTRL_MODE:%d,A/S:%d,U_A/S:%d,"
					"LOCK:%d,Alm:%d,R:%d,\n\n",
					SYSTEM.OPERATION.CTRLMODE, SYSTEM.OPERATION.STATUS, CounterModule.STATUS,
					SYSTEM.OPERATION.LOCKMODE, RcvAlarm.ALARM_CONTROL, RECOVERY.flag);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);


	//Line 10:Detail
	sprintf(MESG,	"Detail. C:%3d,M:%3d, UpdateMode:%3d\n",
					gCenter,gMax,SYSTEM.OPERATION.DETAIL_MODE);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	//Line 11:Counter
	sprintf(MESG,	"ETC. RecoveryOffset:%13.2f,Current:%13.2f,CurrentAll:%13.2f\n"
					"     Detail:%13.2f, 8KFilter:%13.2f,%13.2f\n",
					(float)Recovery_Offset, (float)gCurrent, (float)gCurrentAll, (float)gDetail,
					(float)gInputData,(float)gOutputData);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	
	//Line : Aging Table
	sprintf(MESG,	"Aging. Num:%4d,Index:%4d,Size:%4d\n"
					"       A0:%13.2f,A1:%13.2f,A2:%13.2f\n",
					HoldoverCONTROL.NUMofAgingTable, HoldoverCONTROL.AgingTableIndex, HoldoverCONTROL.AgingTableSize,
					(float)Aging_sol[0],(float)Aging_sol[1],(float)Aging_sol[2]);
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	


	//---------------------------------------------------------------------------------------------------
	sprintf(MESG,"M_TOD>> S:0x%02X E:0x%02X O:0x%02X I:0x%02X D:0x%02X R:0x%02X\n", 
		TOD.status, TOD.error, TOD.opmode, TOD.inform, dual_data.delay, dual_data.reserved[0]);
 	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	sprintf(MESG,"U_TOD>> S:0x%02X E:0x%02X O:0x%02X I:0x%02X D:0x%02X R:0x%02X C:%d,%d\n\n", 
		CounterModule.status, CounterModule.error, CounterModule.opmode, CounterModule.inform,
		CounterModule.delay,psda.your_priority_value,utod_status_array[0],utod_status_array[1]);
 	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	//---------------------------------------------------------------------------------------------------
	
	//Line : FPGA
	for(i=0;i<50;i++){
		
		data = (unsigned char)(GetFpgaValue(i));
		
		sprintf(MESG,	"0x%02X",data);
		TransmitMessage((BYTE*)MESG,PORT_CONTROL);

		if( i!=0  && (i+1)%10 == 0 ){ strcpy(MESG," \n"); }
		else{
			strcpy(MESG,",");			
		}
		TransmitMessage((BYTE*)MESG,PORT_CONTROL);
	}
	strcpy(MESG," \n");
	TransmitMessage((BYTE*)MESG,PORT_CONTROL);

}


///////////////////////////////////
///////////////////////////////////

int bAlarmFalg = 0;
int iAlarmUdpReport = 0;
st_alarm_alm buf_alm_prev;
KTI_SubData_Alm_T buf_alm,buf_alm_saved;

void RecordAlarmEvent(KTI_SubData_Alm_T *ptr)
{
	st_alarm *pe= (st_alarm *)&ptr->event;
	st_alarm *pv= (st_alarm *)&ptr->value;

	if( pe->ant_open ){
		if( pv->ant_open ){
			LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_Antenna_Open, 0);
		}
		else LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_Antenna_OK, 0);
			//LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "bakcup_config fork");
	}

	if( pe->ant_short ){
		if( pv->ant_short ){
			LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_Antenna_Short, 0);
		}
		else LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_Antenna_OK, 0);
	}

	if( pe->gps_holdover ){
		if( pv->gps_holdover ){
			LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_Holdover, 0);
		}
		else LogIt(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_Lock, 0);

	}

	if( pe->ocxo_efc ){
		if( pv->ocxo_efc ){ log_efc_err(); }
		else { log_efc_ok(); }
	}

	if( pe->ocxo_fault ){
		if( pv->ocxo_fault ){ LogIt(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_OCXO_Error,0); }
		else { LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Good: OCXO"); }
	}

	if( pe->clk_na ){
		if( pv->clk_na ){ LogItString(LOG_CATA1_NORM, LOG_CATA2_WARN, "Clock is not available"); }
		else { LogItString(LOG_CATA1_NORM, LOG_CATA2_OK, "Clock is available"); }
	}
}

int bAssignAlarmFirst = 1;
void AssignAlarm()
{
	//KTI_SubData_Report_T *p=&buf_report;
//	st_alarm b;
	int alarm=0;

	// Each value set sub function... for led sync...
//	p->alarm.value.b.ant_open = SYSTEM.EB.ANTENNA == ANTENNA_OPENED;
//	p->alarm.value.b.ant_short = SYSTEM.EB.ANTENNA == ANTENNA_SHORTED;
//	p->alarm.value.b.clk_na = SYSTEM.OPERATION.STATUS!=STATUS_ACTIVE;
//	p->alarm.value.b.gps_holdover = SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN ;
//	p->alarm.value.b.ocxo_efc = DIAG.EFC!=OK;
//	p->alarm.value.b.ocxo_fault = DIAG._10MHz == FAIL;

	buf_alm.event.c = buf_alm_prev.c ^ buf_alm.value.c;
	
	if( bAssignAlarmFirst ){ buf_alm_saved.value.c = 0; bAssignAlarmFirst = 0; }
	
	//udp_printf("alarm>>>>0x%02X = p:0x%02X c:0x%02X \n"
	//		,(int)buf_alm.event.c , (int)buf_alm_prev.c , (int)buf_alm.value.c);
	//if( buf_alm.event.c != 0 && buf_alm.value.c!=0 && GC.RunningTime > 5){
	if( (buf_alm.value.c != buf_alm_saved.value.c) && GC.RunningTime > 5 && !bAlarmFalg ){
		bAlarmFalg =  1;   iAlarmUdpReport = 32;
		buf_alm_saved.event.c = buf_alm.value.c ^ buf_alm_saved.value.c;
		buf_alm_saved.value.c = buf_alm.value.c ;
		udp_printf("------>alarm==>evnt:0x%02X, v:0x%02X\n",buf_alm_saved.event.c, buf_alm_saved.value.c);
	}

	if(buf_alm.event.c != 0 )
	{
//		b = buf_alm.event.b;
//		printf("\n>>>\n""evnt:0x%02X>>,%d,%d ,%d,%d,%d,%d\n",buf_alm.event.c&0xFF,b.ocxo_efc ,b.gps_holdover 	,b.ant_short, b.ant_open, b.clk_na, b.ocxo_fault);
//		b = buf_alm.value.b;
//		printf("valu:0x%02X>>,%d,%d ,%d,%d,%d,%d\n",buf_alm.value.c&0xFF,b.ocxo_efc ,b.gps_holdover 	,b.ant_short, b.ant_open, b.clk_na, b.ocxo_fault);
		RecordAlarmEvent( &buf_alm );
	}

	buf_alm_prev.c = buf_alm.value.c;

}

void SetUpdMsgValue()
{

	KTI_SubData_Report_T *p=&buf_report;
	/////////////////////
	/////////////////////
	int i, ss_max=0,ss_min=0, ss_sum = 0, ss_mean=0,ss_num=0;
	int b_first=1;
	int n_prn	= ((CONFIG.RcvType==1)?8:10);
	for(i=0; i<n_prn; i++){
		if( SYSTEM.TRACK.SAT[i].SS > 0 ){
			if( b_first ){
				b_first = 0;
				ss_min = ss_max = SYSTEM.TRACK.SAT[i].SS;
			}
			ss_sum+=SYSTEM.TRACK.SAT[i].SS;
			ss_num++;
			ss_min = min(ss_min, SYSTEM.TRACK.SAT[i].SS );
			ss_max = max(ss_max, SYSTEM.TRACK.SAT[i].SS );
		}
	}
	if( ss_num )ss_mean = ss_sum/ss_num;
	else ss_mean = 0;
	////////////////////
	////////////////////
	strncpy(buf_ipc.master_id ,GetMasterID(),10);
	buf_ipc.nickname  = M_KTI_NICK_NAME;
	buf_ipc.version[0]= M_KTI_NMS_VERSION;
	buf_ipc.version[1]= M_KTI_RCU_VERSION;
	//buf_ipc.version[1] = M_RCU_VERSION;
	memset(buf_ipc.equip_no,0x80,8);

	//p->alarm.event.c = p->alarm.value.c ^ buf_alm_prev.c;
	p->alarm = buf_alm;
	p->alarm.event.c = 0xFC;
	p->alarm.rsvd1=0x8080;
	p->alarm.rsvd2=0x8080;
	//buf_alm_prev.c = p->alarm.value.c;

	p->period = GetReportPeriod();
	p->dac = (int)CONTROL.RealDAC;
	//p->flag = ;
	if( 		SYSTEM.EB.ANTENNA == ANTENNA_OPENED )	p->gps_ant = 3 ;
	else if( 	SYSTEM.EB.ANTENNA == ANTENNA_SHORTED )	p->gps_ant = 2 ;
	else 												p->gps_ant = 1 ;

	p->gps_cn_max = ss_max;
	p->gps_cn_mean= ss_mean ;
	p->gps_cn_min = ss_min;
	//p->gps_dop = SYSTEM.TRACK.DOP;
	p->gps_visible = SYSTEM.TRACK.VISIBLE;
	p->gps_tracking = SYSTEM.TRACK.TRACKED;

	//if( p->alarm.value.b.gps_holdover && CONTROL.Run_Mode != FREE_RUN ) p->holdover_sec = CHECKLOCK.HoldoverCounter;
	if( p->alarm.value.b.gps_holdover && OnceActiveCondition() ) p->holdover_sec = CHECKLOCK.HoldoverCounter;
	else p->holdover_sec = 0;

	strncpy(p->id ,GetMasterID(),10);
	//p->leap_sec = SYSTEM.TIME.LEAPSECOND;
//	p->offset = (int)(CONTROL.TimeOffset*(double)BILLION);
//	p->freq = (int)(CONTROL.Frequency*(double)1e12);
	//p->offset = (int)(CONTROL.TimeOffset*BILLION);
	//p->freq = (int)(CONTROL.Frequency*1e12);

	if( holdover_time_after24H == HOLDOVER24_YES ) p->status = 4;
	else if( holdover_time_after24H == HOLDOVER24_NOT_YET ) p->status = 3;
	else if( SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION && STATUS_Warmup != ON ) p->status = 2;
	else p->status = 1;

	char str_tmp[30];
	if( p->status < 3) sprintf(str_tmp,"%+2.1e",CONTROL.Frequency );
	else strcpy(str_tmp,"Holdover");
	strncpy(p->freq,str_tmp,8);
	p->running_sec = GC.RunningTime;
	strncpy(p->sn,GetSerialNumber(),15) ;

	p->supplier1 = 0x50;
	p->supplier2 = 0x50;
	p->temperature = (int8)((int)ts.board_temp);
	//p->time_mode = STATUS.TIMEMODE +1;
	p->time_valid = SYSTEM.TIME.Valid == YES;

	if( SYSTEM.EB.STATUS == EB_STATUS_RUN ) p->RcvComm = 1;
	else p->RcvComm = 0;
	p->rsvd1=0x8080;
	memset(p->rsvd3,0x80,8);

}
